﻿// Test_SIPServer.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <Windows.h>
#include <tchar.h>
#include <list>
using namespace std;
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/XyRyNet_ProtocolHdr.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine/NetEngine_Core/NetCore_Define.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine/NetEngine_Core/NetCore_Error.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine_RfcComponents/RfcComponents_Sip/RfcSip_Define.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine_RfcComponents/RfcComponents_Sip/RfcSip_Error.h"

#pragma comment(lib,"Ws2_32.lib")
#pragma comment(lib,"../../../../NetEngine_Windows/NetEngine_SourceCode/Debug/NetEngine_Core.lib")
#pragma comment(lib,"../../../../NetEngine_Windows/NetEngine_SourceCode/Debug/RfcComponents_Sip.lib")

XNETHANDLE xhToken = 0;
int Test_SIPRegister()
{
	TCHAR tszMsgBuffer[2048];
	SIPPROTOCOL_HDRINFO st_SIPProtocol;
	LPCTSTR lpszCodeFile = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\SipCode.types");
	LPCTSTR lpszRegBuffer = "REGISTER sip:xyry.org;transport=tls SIP/2.0\r\n"
		"Via: SIP/2.0/TLS 192.168.1.104:52400;branch=z9hG4bKccc1e88d4ab68cf2;rport\r\n"
		"Via: SIP/2.0/UDP 192.168.2.111:52400;branch=z9hG4bKcc35138d4ab68cf2;rport\r\n"
		"Contact: <sip:1001-0x20d1a38@192.168.1.104:52400;transport=tls>;expires=3600\r\n"
		"Max-Forwards: 70\r\n"
		"To: <sip:1001@xyry.org:5061>\r\n"
		"From: <sip:1001@xyry.org:5061>;tag=24174dec803066c5\r\n"
		"Call-ID: 8f8e5fb8ce260308\r\n"
		"CSeq: 62430 REGISTER\r\n"
		"User-Agent: 1.0.2\r\n"
		"Allow: INVITE,ACK,BYE,CANCEL,OPTIONS,REFER,NOTIFY,SUBSCRIBE,INFO,MESSAGE\r\n"
		"Keepalive: 30\r\n"
		"Content-Length: 0\r\n\r\n";

	memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
	memset(&st_SIPProtocol, '\0', sizeof(SIPPROTOCOL_HDRINFO));
	_tcscpy(tszMsgBuffer, lpszRegBuffer);
	int nLen = _tcslen(lpszRegBuffer);
	int nMsgLen = 2048;

	RfcComponents_SipProtocol_Parse(tszMsgBuffer, nLen, &st_SIPProtocol);

	memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
	st_SIPProtocol.st_Response.nCode = 200;
	_tcscpy(st_SIPProtocol.st_Response.tszMethod, "OK");

	RfcComponents_SipProtocol_PacketResponse(&st_SIPProtocol, tszMsgBuffer, &nMsgLen);

	return 0;
}
void __stdcall NetCore_CBRecv(LPCSTR lpszClientAddr, SOCKET hSocket, LPCSTR lpszRecvMsg, int nMsgLen, LPVOID lParam)
{
	int nRecvLen = nMsgLen;
	TCHAR tszMsgBuffer[2048];
	TCHAR tszToAddr[64];
	SIPPROTOCOL_HDRINFO st_SIPProtocol;

	memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
	memset(tszToAddr, '\0', sizeof(tszToAddr));
	memset(&st_SIPProtocol, '\0', sizeof(SIPPROTOCOL_HDRINFO));

	if (!RfcComponents_SipServer_Exist(lpszClientAddr))
	{
		RfcComponents_SipServer_Create(lpszClientAddr);
		printf("用户进入:%s\n", lpszClientAddr);
	}
	RfcComponents_SipProtocol_Parse(lpszRecvMsg, nRecvLen, &st_SIPProtocol);
	RfcComponents_SipServer_Process(lpszClientAddr, &st_SIPProtocol);

	if (st_SIPProtocol.bRequest)
	{
		if (0 == _tcsnicmp(XENGINE_RFCCOMPONENTS_SIP_PROTOCOL_STR_TYPE_INVITE, st_SIPProtocol.st_Request.tszMethod, _tcslen(XENGINE_RFCCOMPONENTS_SIP_PROTOCOL_STR_TYPE_INVITE)) || 0 == _tcsnicmp(XENGINE_RFCCOMPONENTS_SIP_PROTOCOL_STR_TYPE_MESSAGE, st_SIPProtocol.st_Request.tszMethod, _tcslen(XENGINE_RFCCOMPONENTS_SIP_PROTOCOL_STR_TYPE_MESSAGE)) || 0 == _tcsnicmp(XENGINE_RFCCOMPONENTS_SIP_PROTOCOL_STR_TYPE_OPTIONS, st_SIPProtocol.st_Request.tszMethod, _tcslen(XENGINE_RFCCOMPONENTS_SIP_PROTOCOL_STR_TYPE_OPTIONS)))
		{
			//呼叫请求
			if (RfcComponents_SipServer_FindAddr(st_SIPProtocol.st_To.tszName, tszToAddr))
			{
				RfcComponents_SipProtocol_PacketResponse(&st_SIPProtocol, tszMsgBuffer, &nRecvLen);
				NetCore_UDPIocp_SendMsgEx(xhToken, lpszClientAddr, tszMsgBuffer, &nRecvLen);

				NetCore_UDPIocp_SendMsgEx(xhToken, tszToAddr, lpszRecvMsg, &nMsgLen);
			}
			else
			{
				st_SIPProtocol.st_Response.nCode = 404;
				RfcComponents_SipConfig_GetCode(st_SIPProtocol.st_Response.nCode, st_SIPProtocol.st_Response.tszMethod);

				RfcComponents_SipServer_SetStatus(lpszClientAddr);//还原状态
				RfcComponents_SipProtocol_PacketResponse(&st_SIPProtocol, tszMsgBuffer, &nRecvLen);
				NetCore_UDPIocp_SendMsgEx(xhToken, lpszClientAddr, tszMsgBuffer, &nRecvLen);
			}
		}
		else if (0 == _tcsnicmp(XENGINE_RFCCOMPONENTS_SIP_PROTOCOL_STR_TYPE_ACK, st_SIPProtocol.st_Request.tszMethod, _tcslen(XENGINE_RFCCOMPONENTS_SIP_PROTOCOL_STR_TYPE_ACK)))
		{
			//确认请求
			if (RfcComponents_SipServer_FindAddr(st_SIPProtocol.st_To.tszName, tszToAddr))
			{
				NetCore_UDPIocp_SendMsgEx(xhToken, tszToAddr, lpszRecvMsg, &nMsgLen);
			}
		}
		else
		{
			RfcComponents_SipProtocol_PacketResponse(&st_SIPProtocol, tszMsgBuffer, &nRecvLen);
			NetCore_UDPIocp_SendMsgEx(xhToken, lpszClientAddr, tszMsgBuffer, &nRecvLen);
		}
	}
	else
	{
		/*
		ENUM_RFCCOMPONENTS_SIP_EVENTS enSIPEvent;
		RfcComponents_SipServer_GetStatus(lpszClientAddr, &enSIPEvent);
		if ((enSIPEvent >= ENUM_RFCCOMPONENTS_SIP_EVENTS_CALL_PROCEEDING) && (ENUM_RFCCOMPONENTS_SIP_EVENTS_CALL_RINGING <= enSIPEvent))
		{
			//需要转发
			if (RfcComponents_SipServer_FindAddr(st_SIPProtocol.st_From.tszName, tszToAddr))
			{
				NetCore_UDPIocp_SendMsgEx(xhToken, tszToAddr, lpszRecvMsg, &nRecvLen);
				printf("%s\n", lpszRecvMsg);
			}
		}
		*/
		//需要转发
		if (RfcComponents_SipServer_FindAddr(st_SIPProtocol.st_From.tszName, tszToAddr))
		{
			NetCore_UDPIocp_SendMsgEx(xhToken, tszToAddr, lpszRecvMsg, &nRecvLen);
			printf("%s\n", lpszRecvMsg);
		}
		
	}
	printf("用户发送数据:%s\n", lpszClientAddr);
}

int main()
{
	Test_SIPRegister();
	WSADATA st_WSAData;
	WSAStartup(MAKEWORD(2, 2), &st_WSAData);
	LPCTSTR lpszCodeFile = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\SipCode.types");
	if (!NetCore_UDPIocp_StartEx(&xhToken))
	{
		printf("错误\n");
		return -1;
	}
	NetCore_UDPIocp_RegisterCallBackEx(xhToken, NetCore_CBRecv);

	RfcComponents_SipServer_Init("xyry.org", lpszCodeFile, NULL, TRUE);
	RfcComponents_SipServer_UserInsert("bob", "123123");
	RfcComponents_SipServer_UserInsert("alice", "123123");
	printf("启动服务成功\n");
	while (1)
	{
		Sleep(111);
	}
	WSACleanup();
	return 0;
}
